package com.gilt;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import org.archive.crawler.datamodel.CrawlURI;
import org.archive.net.UURI;
import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.HasParentFilter;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.tags.InputTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import com.dao.DBUtil;
import com.dao.DealBean;
import com.mongodb.MongoException;
import com.util.CategoryMapping;

public class GiltHtmlParser {
	
	public  void testParser(String filePath, CrawlURI curi) throws UnknownHostException, MongoException {  
        
		DealBean giltBean = new DealBean();

		try {  
			String skuId = "";
			
            Parser parser = new Parser(filePath); 
            
            parser.setEncoding("UTF-8"); 
            
            UURI uuri = curi.getUURI();
            
            giltBean.setBuy_url(uuri.toString());
            
            if(curi.getCategory2()!=null){
            	
            	System.out.println("====="+ curi.getCategory2());
            	
            	 String category2 = CategoryMapping.getType2(curi.getCategory2());;
                 
                 giltBean.setCategory2(category2);
            }
            else  giltBean.setCategory2("");
            
            NodeFilter skuIdNodeFilter = new AndFilter(new TagNameFilter("input"),new HasAttributeFilter("id", "product-look-id"));
            
            NodeList skuIdNodeList = parser.extractAllNodesThatMatch(skuIdNodeFilter); 
            
            if (skuIdNodeList != null && skuIdNodeList.size() > 0) {  

                Node skuIdNode = skuIdNodeList.elementAt(0);  

                skuId = ((InputTag)skuIdNode).getAttribute("value");  

                giltBean.setSku_id(skuId);
                
            }
            
            parser.reset();
            
            NodeFilter imageFilter = new NodeClassFilter(ImageTag.class);
            
            NodeFilter imageNodeFilter = new HasParentFilter(new TagNameFilter("div"));

            NodeList imageTagList = parser.extractAllNodesThatMatch(imageFilter);
            
            NodeList imageNodeList = imageTagList.extractAllNodesThatMatch(imageNodeFilter);
            
            if (imageNodeList != null && imageNodeList.size() > 0) {  

                ImageTag imageNode = (ImageTag) imageNodeList.elementAt(0);
                
                String imgUrl = imageNode.getImageURL().replace("file", "http");
                
//                try {
//					saveFile(imgUrl, skuId);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}

                giltBean.setImage_url(imgUrl);  

            }
            
            parser.reset();
            
            NodeFilter productNameNodeFilter = new AndFilter(new TagNameFilter("h1"),new HasAttributeFilter("class", "product-name"));
            
            NodeList productNameNodeList = parser.extractAllNodesThatMatch(productNameNodeFilter);
             
              if (productNameNodeList != null && productNameNodeList.size() > 0) {  

                  Node productNameNode = productNameNodeList.elementAt(0);  

                  String productName = productNameNode.toPlainTextString().trim();  

                  giltBean.setProduct_name(productName); 

              }  

            parser.reset();
            
            NodeFilter salePriceNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "price-emphasis"));
            
            NodeList salePriceNodeList = parser.extractAllNodesThatMatch(salePriceNodeFilter);
            
            if (salePriceNodeList != null && salePriceNodeList.size() > 0) {  
          	  
                Node salePriceNode = salePriceNodeList.elementAt(0);  

                String salePrice = salePriceNode.toPlainTextString().trim();  

                giltBean.setSale_price(salePrice);

            }
            
            parser.reset();
            
            NodeFilter originalPriceNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "msrp"));
            
            NodeList originalPriceNodeList = parser.extractAllNodesThatMatch(originalPriceNodeFilter);
            
            if (originalPriceNodeList != null && originalPriceNodeList.size() > 0) {  
          	  
                Node originalPriceNode = originalPriceNodeList.elementAt(0);  

                String originalPrice = originalPriceNode.toPlainTextString().trim();  

                giltBean.setOriginal_price(originalPrice);

            } 
            
            parser.reset();
            
            NodeFilter brandNodeFilter = new AndFilter(new TagNameFilter("a"),new HasAttributeFilter("class", "brand-name-text"));
            
            NodeList brandNodeList = parser.extractAllNodesThatMatch(brandNodeFilter);
            
            if (brandNodeList != null && brandNodeList.size() > 0) {  
          	  
                Node brandNode = brandNodeList.elementAt(0);  

                String brand = brandNode.toPlainTextString().trim();  

                giltBean.setBrand(brand);

            }  
            
            parser.reset();
            
            NodeFilter categoryNodeFilter = new AndFilter(new TagNameFilter("a"),new HasAttributeFilter("class", "breadcrumb-link"));
            
            NodeList categoryNodeList = parser.extractAllNodesThatMatch(categoryNodeFilter);
            
            if (categoryNodeList != null && categoryNodeList.size() > 0) {  
          	  
                Node categoryNode = categoryNodeList.elementAt(0);  

                String category1 = CategoryMapping.getType1(categoryNode.toPlainTextString().trim());  

                giltBean.setCategory1(category1);

            } 
            
            parser.reset();
            
            NodeFilter endsTimeNodeFilter = new AndFilter(new TagNameFilter("span"),new HasAttributeFilter("class", "sale-countdown-interval"));
            
            NodeList endsTimeNodeList = parser.extractAllNodesThatMatch(endsTimeNodeFilter);
            
            if (endsTimeNodeList != null && endsTimeNodeList.size() > 0) {  
          	  
                Node endsTimeNode = endsTimeNodeList.elementAt(0);  

                String endsTime = endsTimeNode.toPlainTextString().trim();  

                giltBean.setEnds_time(endsTime);

            } 
            parser.reset();
            NodeFilter productDescriptionFilter1 = new TagNameFilter("ul");
            
            NodeList productDescriptionNodeList1 = parser.extractAllNodesThatMatch(productDescriptionFilter1);
            
            String productDescription1 = productDescriptionNodeList1.elementAt(21).toPlainTextString().trim();
            
            parser.reset();
            
            NodeFilter productDescriptionFilter2 = new TagNameFilter("p");
            
            NodeList productDescriptionNodeList2 = parser.extractAllNodesThatMatch(productDescriptionFilter2);
            
            StringBuffer buf = new StringBuffer();
            
            buf.append(productDescriptionNodeList2.elementAt(1).toPlainTextString().trim());
            
            buf.append("\n");
            buf.append(productDescription1);
            buf.append("\n");
            buf.append(productDescriptionNodeList2.elementAt(2).toPlainTextString().trim());
            buf.append("\n");
            buf.append(productDescriptionNodeList2.elementAt(3).toPlainTextString().trim());
            buf.append("\n");
            buf.append(productDescriptionNodeList2.elementAt(4).toPlainTextString().trim());
            buf.append("\n");
            buf.append(productDescriptionNodeList2.elementAt(5).toPlainTextString().trim());
            buf.append(productDescriptionNodeList2.elementAt(6).toPlainTextString().trim());
            
            giltBean.setProduct_description(buf.toString());
            
            String modelId = "";
            
            giltBean.setModel_id(modelId);
            
            String souceSite = "Gilt";
            
            giltBean.setSouce_site(souceSite);


          }catch(ParserException e){  

            e.printStackTrace();  

          }  
          
          DBUtil db = new DBUtil();
          
          if(giltBean.getSku_id()!=null){
          
          db.save(giltBean);
          }

  }  
	 private static void saveFile(String imgString, String productId) throws Exception {
		  	URL url;
		  	File outFile;
		  	OutputStream os = null;
		  	InputStream is = null;
				try {
					url = new URL(imgString);
					outFile = new File("C:/gilt/img/"+productId+".jpg");
					os = new FileOutputStream(outFile);
					is = url.openStream();
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				byte[] buff = new byte[1024];
				while(true) {
					int readed = is.read(buff);
					if(readed == -1) {
						break;
					}
					byte[] temp = new byte[readed];
					System.arraycopy(buff, 0, temp, 0, readed);
					os.write(temp);
				}
				is.close(); 
		      os.close();
			}

    

 
}
